package org.teamh.gui.ingame;
import java.util.HashMap;

import org.teamh.gui.GUI;
import org.teamh.logic.datamodel.*;

import de.lessvoid.nifty.controls.DropDown;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.TextRenderer;

public class Information {
	private GUI gui;
	private DatamodelManager model;
	private IngameController ingameController;
	public int creep_value;
	public int tower_value;


	public Information(GUI gui, IngameController ingameController) {
		this.gui = gui;
		model = gui.getStart().getLogic().getManager();
		this.ingameController=ingameController;
	}

	public void informations(String object_name, String objectIdentity, boolean build) {
		gui.getIngameScreen().getIngameFunctions().getMap().selectedTower(object_name);
		if(objectIdentity.contains("creep")){
			try {
				HashMap<String, CreepDescription> creep = model.getCreepDescriptions();
				for (String creepDescription : creep.keySet()) {
					if(creep.get(creepDescription).getName().contains(object_name)){
						if((ingameController.getCreepChoosed()==null && ingameController.getGrayCreepChoosed() == null)||!build){
							Element creep_name = gui.getNifty().getCurrentScreen().findElementByName("CreepName");
							creep_name.getRenderer(TextRenderer.class).setText("-");
							
							Element creep_price = gui.getNifty().getCurrentScreen().findElementByName("PriceValue");
							creep_price.getRenderer(TextRenderer.class).setText("-");
							
							Element creep_heath= gui.getNifty().getCurrentScreen().findElementByName("HealthValue");
							creep_heath.getRenderer(TextRenderer.class).setText("-");
							
							Element creep_speed= gui.getNifty().getCurrentScreen().findElementByName("SpeedValue");
							creep_speed.getRenderer(TextRenderer.class).setText("-");
							
							Element creep_income= gui.getNifty().getCurrentScreen().findElementByName("IncomeValue");
							creep_income.getRenderer(TextRenderer.class).setText("-");
							
							Element creep_bounty= gui.getNifty().getCurrentScreen().findElementByName("BountyValue");
							creep_bounty.getRenderer(TextRenderer.class).setText("-");
							
						}else{
						Element creep_name = gui.getNifty().getCurrentScreen().findElementByName("CreepName");
						creep_name.getRenderer(TextRenderer.class).setText(creep.get(creepDescription).getName()+"");
						
						Element creep_price = gui.getNifty().getCurrentScreen().findElementByName("PriceValue");
						creep_price.getRenderer(TextRenderer.class).setText(creep.get(creepDescription).getPrice()+"");
						
						Element creep_heath= gui.getNifty().getCurrentScreen().findElementByName("HealthValue");
						creep_heath.getRenderer(TextRenderer.class).setText(creep.get(creepDescription).getHealthMax()+"");
						
						Element creep_speed= gui.getNifty().getCurrentScreen().findElementByName("SpeedValue");
						creep_speed.getRenderer(TextRenderer.class).setText(creep.get(creepDescription).getSpeedMax()+"");
						
						Element creep_income= gui.getNifty().getCurrentScreen().findElementByName("IncomeValue");
						creep_income.getRenderer(TextRenderer.class).setText(creep.get(creepDescription).getIncome()+"");
						
						Element creep_bounty= gui.getNifty().getCurrentScreen().findElementByName("BountyValue");
						creep_bounty.getRenderer(TextRenderer.class).setText(creep.get(creepDescription).getBounty()+"");
						}
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}

		
		if(objectIdentity.contains("tower")){
			try {
				HashMap<String, TowerDescription> tower = model.getTowerDescriptions();
				for(String towerDescription: tower.keySet()){
					if(tower.get(towerDescription).getName().contains(object_name)&&tower.get(towerDescription).getLevel()==1){
						if((ingameController.getTowerChoosed() == null && ingameController.getGrayTowerChoosed() == null)||!build){
							Element tower_name = gui.getNifty().getCurrentScreen().findElementByName("TowerName");
							tower_name.getRenderer(TextRenderer.class).setText("-");
							
							Element tower_level = gui.getNifty().getCurrentScreen().findElementByName("TowerLevel");
							tower_level.getRenderer(TextRenderer.class).setText("-");
							
							Element tower_price = gui.getNifty().getCurrentScreen().findElementByName("PriceTowerValue");
							tower_price.getRenderer(TextRenderer.class).setText("-");
							
							Element tower_cooldown = gui.getNifty().getCurrentScreen().findElementByName("CooldownTowerValue");
							tower_cooldown.getRenderer(TextRenderer.class).setText("-");
							
							Element tower_range = gui.getNifty().getCurrentScreen().findElementByName("RangeValue");
							tower_range.getRenderer(TextRenderer.class).setText("-");
							
							Element tower_damage = gui.getNifty().getCurrentScreen().findElementByName("TowerDamageValue");
							tower_damage.getRenderer(TextRenderer.class).setText("-");
							
							Element tower_splashDamageReduce = gui.getNifty().getCurrentScreen().findElementByName("SplashDamageReduceValue");
							tower_splashDamageReduce.getRenderer(TextRenderer.class).setText("-");
							
							Element tower_splashradius = gui.getNifty().getCurrentScreen().findElementByName("SplashRadiusValue");
							tower_splashradius.getRenderer(TextRenderer.class).setText("-");
							
							Element tower_slowrate = gui.getNifty().getCurrentScreen().findElementByName("SlowrateValue");
							tower_slowrate.getRenderer(TextRenderer.class).setText("-");
							
							Element tower_slowtime = gui.getNifty().getCurrentScreen().findElementByName("SlowtimeValue");
							tower_slowtime.getRenderer(TextRenderer.class).setText("-");
						}else{
							Element tower_name = gui.getNifty().getCurrentScreen().findElementByName("TowerName");
							tower_name.getRenderer(TextRenderer.class).setText(tower.get(towerDescription).getName()+"");
							
							Element tower_level = gui.getNifty().getCurrentScreen().findElementByName("TowerLevel");
							tower_level.getRenderer(TextRenderer.class).setText("Lv 1");
							
							Element tower_price = gui.getNifty().getCurrentScreen().findElementByName("PriceTowerValue");
							tower_price.getRenderer(TextRenderer.class).setText(tower.get(towerDescription).getPrice()+"");
							
							Element tower_cooldown = gui.getNifty().getCurrentScreen().findElementByName("CooldownTowerValue");
							tower_cooldown.getRenderer(TextRenderer.class).setText(tower.get(towerDescription).getCoolDownMax()+"");
							
							Element tower_range = gui.getNifty().getCurrentScreen().findElementByName("RangeValue");
							tower_range.getRenderer(TextRenderer.class).setText(tower.get(towerDescription).getRange()+"");
							
							Element tower_splashDamageReduce = gui.getNifty().getCurrentScreen().findElementByName("SplashDamageReduceValue");
							tower_splashDamageReduce.getRenderer(TextRenderer.class).setText(tower.get(towerDescription).getSplashDamageReduce()+"");
							
							Element tower_splashradius = gui.getNifty().getCurrentScreen().findElementByName("SplashRadiusValue");
							tower_splashradius.getRenderer(TextRenderer.class).setText(tower.get(towerDescription).getSplashRadius()+"");
							
							Element tower_damage = gui.getNifty().getCurrentScreen().findElementByName("TowerDamageValue");
							tower_damage.getRenderer(TextRenderer.class).setText(tower.get(towerDescription).getDamage()+"");
							
							Element tower_slowrate = gui.getNifty().getCurrentScreen().findElementByName("SlowrateValue");
							tower_slowrate.getRenderer(TextRenderer.class).setText(tower.get(towerDescription).getSlowRate()+"");
							
							Element tower_slowtime = gui.getNifty().getCurrentScreen().findElementByName("SlowtimeValue");
							tower_slowtime.getRenderer(TextRenderer.class).setText(tower.get(towerDescription).getSlowTime()+"");
						}
						
					}
				}
				
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
	}
	
	public void setValueCreep(int value, boolean build) {
		this.creep_value = value;
		String object = ObjectPath.creepDescription(creep_value);
		informations(object, "creep", build);
		
	}

	public void setValueTower(int value, boolean build) {
		this.tower_value = value;
		String object = ObjectPath.tower(tower_value);
		informations(object, "tower", build);
	}
	
	public void setValueForTowerUpgrade(Tower tower) {
		for(TowerDescription t: model.getTowerDescriptions().values()) {
			if(t.getType().equals(tower.getType()) && t.getLevel() == tower.getLevel()) {
				if(tower.getType().equals("Ultimate")) {
					if(tower.getLevel() == 1) {
						for(TowerDescription t_next: model.getTowerDescriptions().values()) {
							if(t_next.getType().equals(tower.getType()) && t_next.getLevel() == tower.getLevel() + 1) {
								showInfosWithUpgrade(t, t_next, tower);
							}
						}
					} else {
						showInfosWithoutUpgrade(t, tower);
					}
				} else {
					if(tower.getLevel() < 4) {
						for(TowerDescription t_next: model.getTowerDescriptions().values()) {
							if(t_next.getType().equals(tower.getType()) && t_next.getLevel() == tower.getLevel() + 1) {
								showInfosWithUpgrade(t, t_next, tower);
							}
						}
					} else {
						showInfosWithoutUpgrade(t, tower);
					}
				}
			}
		}	
	}

	@SuppressWarnings("unchecked")
	private void showInfosWithoutUpgrade(TowerDescription t, Tower tower) {
		Element twr = gui.getNifty().getCurrentScreen().findElementByName("TowerName");
		twr.getRenderer(TextRenderer.class).setText(t.getName());
		twr = gui.getNifty().getCurrentScreen().findElementByName("TowerName Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getName());

		twr = gui.getNifty().getCurrentScreen().findElementByName("TowerLevel");
		twr.getRenderer(TextRenderer.class).setText(" Lv " + t.getLevel());
		twr = gui.getNifty().getCurrentScreen().findElementByName("TowerLevel Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(" Lv " + t.getLevel());
		
		twr = gui.getNifty().getCurrentScreen().findElementByName("PriceTowerValue");
		twr.getRenderer(TextRenderer.class).setText(t.getPrice() + "");
		twr = gui.getNifty().getCurrentScreen().findElementByName("PriceTowerValue Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getPrice() + "");

		twr = gui.getNifty().getCurrentScreen().findElementByName("CooldownTowerValue");
		twr.getRenderer(TextRenderer.class).setText(t.getCoolDownMax() + "");
		twr = gui.getNifty().getCurrentScreen().findElementByName("CooldownTowerValue Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getCoolDownMax() + "");

		twr = gui.getNifty().getCurrentScreen().findElementByName("RangeValue");
		twr.getRenderer(TextRenderer.class).setText(t.getRange() + "");
		twr = gui.getNifty().getCurrentScreen().findElementByName("RangeValue Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getRange() + "");

		twr = gui.getNifty().getCurrentScreen().findElementByName("TowerDamageValue");
		twr.getRenderer(TextRenderer.class).setText(t.getDamage() + "");
		twr = gui.getNifty().getCurrentScreen().findElementByName("TowerDamageValue Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getDamage() + "");

		twr = gui.getNifty().getCurrentScreen().findElementByName("SplashDamageReduceValue");
		twr.getRenderer(TextRenderer.class).setText(t.getSplashDamageReduce()+"");
		twr = gui.getNifty().getCurrentScreen().findElementByName("SplashDamageReduceValue Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getSplashDamageReduce()+"");
		
		twr = gui.getNifty().getCurrentScreen().findElementByName("SplashRadiusValue");
		twr.getRenderer(TextRenderer.class).setText(t.getSplashRadius()+"");
		twr = gui.getNifty().getCurrentScreen().findElementByName("SplashRadiusValue Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getSplashRadius()+"");
		
		twr = gui.getNifty().getCurrentScreen().findElementByName("SlowrateValue");
		twr.getRenderer(TextRenderer.class).setText(t.getSlowRate() + "");
		twr = gui.getNifty().getCurrentScreen().findElementByName("SlowrateValue Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getSlowRate() + "");

		twr = gui.getNifty().getCurrentScreen().findElementByName("SlowtimeValue");
		twr.getRenderer(TextRenderer.class).setText(t.getSlowTime() + "");
		twr = gui.getNifty().getCurrentScreen().findElementByName("SlowtimeValue Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getSlowTime() + "");

		if(tower.getStratetgy() != null) {
			DropDown<String> tower_Strategy = gui.getNifty().getCurrentScreen().findNiftyControl("dropDown", DropDown.class);
			tower_Strategy.selectItem(tower.getStratetgy());
			tower_Strategy = gui.getNifty().getCurrentScreen().findNiftyControl("dropDown Creep Panel", DropDown.class);
			tower_Strategy.selectItem(tower.getStratetgy());
		}
		
		gui.getNifty().getCurrentScreen().findElementByName("upgradeButton").hide();
		gui.getNifty().getCurrentScreen().findElementByName("upgradeButton Creep Panel").hide();
	}

	@SuppressWarnings("unchecked")
	private void showInfosWithUpgrade(TowerDescription t, TowerDescription t_next, Tower tower) {
		Element twr = gui.getNifty().getCurrentScreen().findElementByName("TowerName");
		twr.getRenderer(TextRenderer.class).setText(t.getName());
		twr = gui.getNifty().getCurrentScreen().findElementByName("TowerName Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getName());
		
		twr = gui.getNifty().getCurrentScreen().findElementByName("TowerLevel");
		twr.getRenderer(TextRenderer.class).setText(" Lv " + t.getLevel() + " (Lv " + t_next.getLevel() + ")");
		twr = gui.getNifty().getCurrentScreen().findElementByName("TowerLevel Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(" Lv " + t.getLevel() + " (Lv " + t_next.getLevel() + ")");

		twr = gui.getNifty().getCurrentScreen().findElementByName("PriceTowerValue");
		twr.getRenderer(TextRenderer.class).setText(t.getPrice() + " (" + t_next.getPrice() + ")");
		twr = gui.getNifty().getCurrentScreen().findElementByName("PriceTowerValue Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getPrice() + " (" + t_next.getPrice() + ")");
		
		if(t_next.getPrice() > Integer.parseInt(gui.getNifty().getCurrentScreen().findElementByName("GoldValue").getRenderer(TextRenderer.class).getOriginalText())) {
			gui.getNifty().getCurrentScreen().findElementByName("upgradeButton").disable();
			gui.getNifty().getCurrentScreen().findElementByName("upgradeButton Creep Panel").disable();
		}

		twr = gui.getNifty().getCurrentScreen().findElementByName("CooldownTowerValue");
		twr.getRenderer(TextRenderer.class).setText(t.getCoolDownMax() + " (" + t_next.getCoolDownMax() + ")");
		twr = gui.getNifty().getCurrentScreen().findElementByName("CooldownTowerValue Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getCoolDownMax() + " (" + t_next.getCoolDownMax() + ")");

		twr = gui.getNifty().getCurrentScreen().findElementByName("RangeValue");
		twr.getRenderer(TextRenderer.class).setText(t.getRange() + " (" + t_next.getRange() + ")");
		twr = gui.getNifty().getCurrentScreen().findElementByName("RangeValue Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getRange() + " (" + t_next.getRange() + ")");

		twr = gui.getNifty().getCurrentScreen().findElementByName("TowerDamageValue");
		twr.getRenderer(TextRenderer.class).setText(t.getDamage() + " (" + t_next.getDamage() + ")");
		twr = gui.getNifty().getCurrentScreen().findElementByName("TowerDamageValue Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getDamage() + " (" + t_next.getDamage() + ")");
		
		twr = gui.getNifty().getCurrentScreen().findElementByName("SplashDamageReduceValue");
		twr.getRenderer(TextRenderer.class).setText(t.getSplashDamageReduce() + " (" + t_next.getSplashDamageReduce() + ")");
		twr = gui.getNifty().getCurrentScreen().findElementByName("SplashDamageReduceValue Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getSplashDamageReduce() + " (" + t_next.getSplashDamageReduce() + ")");
		
		twr = gui.getNifty().getCurrentScreen().findElementByName("SplashRadiusValue");
		twr.getRenderer(TextRenderer.class).setText(t.getSplashRadius() + " (" + t_next.getSplashRadius() + ")");
		twr = gui.getNifty().getCurrentScreen().findElementByName("SplashRadiusValue Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getSplashRadius() + " (" + t_next.getSplashRadius() + ")");

		twr = gui.getNifty().getCurrentScreen().findElementByName("SlowrateValue");
		twr.getRenderer(TextRenderer.class).setText(t.getSlowRate() + " (" + t_next.getSlowRate() + ")");
		twr = gui.getNifty().getCurrentScreen().findElementByName("SlowrateValue Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getSlowRate() + " (" + t_next.getSlowRate() + ")");

		twr = gui.getNifty().getCurrentScreen().findElementByName("SlowtimeValue");
		twr.getRenderer(TextRenderer.class).setText(t.getSlowTime() + " (" + t_next.getSlowTime() + ")");
		twr = gui.getNifty().getCurrentScreen().findElementByName("SlowtimeValue Creep Panel");
		twr.getRenderer(TextRenderer.class).setText(t.getSlowTime() + " (" + t_next.getSlowTime() + ")");
		
		if(tower.getStratetgy() != null) {
			String strategy = tower.getStratetgy().charAt(0) + tower.getStratetgy().substring(1).toLowerCase();
			DropDown<String> tower_Strategy = gui.getNifty().getCurrentScreen().findNiftyControl("dropDown", DropDown.class);
			tower_Strategy.selectItem(strategy);
			tower_Strategy = gui.getNifty().getCurrentScreen().findNiftyControl("dropDown Creep Panel", DropDown.class);
			tower_Strategy.selectItem(strategy);
		}
	}

	public int getTower_value() {
		return tower_value;
	}

}
